package com.mall.service.commodity.service;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mall.commons.common.CommonPage;
import com.mall.model.domain.PmsProductAttribute;
import com.mall.model.domain.PmsProductCategoryAttributeRelation;
import com.mall.service.commodity.mapper.CategoryAndAttuteMapper;
import com.mall.service.commodity.mapper.ProductMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @ClassName ProductAttributeService
 * @Description 属性  参数列表  逻辑层
 * @Author dcq
 * @Date 2023/3/12 11:19
 * @Version 1.0
 */
@Service
public class ProductAttributeService {
    @Autowired
    private ProductMapper productMapper;
    @Autowired
    private CategoryAndAttuteMapper categoryAndAttributIdMapper;
    //欸也查询 属性  参数
    public CommonPage listCid(Integer cid, Integer pageNum, Integer pageSize, Integer type) {
        //分页构造器
        Page<PmsProductAttribute> MyPage = new Page<>(pageNum, pageSize);
        //条件构造器
        LambdaQueryWrapper<PmsProductAttribute>queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(PmsProductAttribute::getProductAttributeCategoryId,cid);
        queryWrapper.eq(type!=null,PmsProductAttribute::getType,type);
        Page<PmsProductAttribute> pmsProductAttributePage = productMapper.selectPage(MyPage, queryWrapper);
        CommonPage page = CommonPage.getPage(pmsProductAttributePage);
        return  page;

    }
    //单个商品属性
    public PmsProductAttribute getByIdSei(Integer id) {
        PmsProductAttribute pmsProductAttribute = productMapper.selectById(id);
        return pmsProductAttribute;
    }
   //修改
    public void updateSei(PmsProductAttribute pmsProductAttribute) {
        productMapper.updateById(pmsProductAttribute);
    }
    //批量删除
    public void deleteSei(Integer[] ids) {
        LambdaQueryWrapper<PmsProductAttribute>queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.in(PmsProductAttribute::getId,ids);
        productMapper.delete(queryWrapper);
    }
    //根据商品分类的id获取商品属性及属性分类
    public List<Map> atttrInfo(Integer productCategoryId) {
        //条件构造 查出商品分类对应的所有属性id
        LambdaQueryWrapper<PmsProductCategoryAttributeRelation>queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(PmsProductCategoryAttributeRelation::getProductCategoryId,productCategoryId);
        List<PmsProductCategoryAttributeRelation> pmsProductCategoryAttributeRelations = categoryAndAttributIdMapper.selectList(queryWrapper);
        List<Long> productAttributeId = CollUtil.getFieldValues(pmsProductCategoryAttributeRelations, "productAttributeId", Long.class);
         //根据属性id找出对应分类id
        LambdaQueryWrapper<PmsProductAttribute>queryWrapperPpa=new LambdaQueryWrapper<>();
        queryWrapperPpa.in(PmsProductAttribute::getId,productAttributeId);
        List<PmsProductAttribute> pmsProductAttributes = productMapper.selectList(queryWrapperPpa);
        List<Map>list=new ArrayList<>();
        for (PmsProductAttribute pmsProductAttribute : pmsProductAttributes) {
            HashMap map = new HashMap<>();
            map.put("attributeId",pmsProductAttribute.getId());
            map.put("attributeCategoryId",pmsProductAttribute.getProductAttributeCategoryId());
            list.add(map);
        }
        return list;
    }
   //增加
    public void creatSei(PmsProductAttribute pmsProductAttribute) {
        productMapper.insert(pmsProductAttribute);
    }
}